package cn.doways.api.controller;

import cn.doways.api.dto.Result;
import cn.doways.api.entity.Student;
import cn.doways.api.service.ToolsService;
import cn.doways.api.utils.ToolsAipFace;
import cn.doways.api.vo.FaceTokenVO;
import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName ToolsController
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-11 00:50
 **/
@RestController
public class ToolsController {


    private FaceTokenVO faceTokenVO;

    private ToolsService toolsService;

    @Autowired
    public ToolsController(ToolsService toolsService,FaceTokenVO faceTokenVO) {
        this.toolsService = toolsService;
        this.faceTokenVO=faceTokenVO;
    }



    @GetMapping("/sms/{phoneNumber}")
    @ApiOperation(value = "根据手机号发送验证短信",tags = {"工具"},notes = "短信发送返回状态有两种404和200，200代表成功，404代表失败，验证码我会存在Session中待密码提交时一并认证Session过期时间5分钟")
    public Result<String> getSms(@PathVariable("phoneNumber") String phoneNumber, HttpSession httpSession){
        String result=toolsService.getSms(phoneNumber);
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            Map json=objectMapper.readValue(result, Map.class);
            if((int)json.get("result")!=0){
                return Result.error("发送短信失败",json.get("errmsg").toString());
            }else {
                httpSession.setAttribute(phoneNumber,json.get("ext"));
                return Result.success(json.get("result").toString());
            }
        }catch (Exception e){
            return Result.error("JSON序列号失败！",e.toString());
        }
    }


    @PreAuthorize("hasRole('STUDENT')")
    @GetMapping("/tools/{id}/face")
    @ApiOperation(value = "获取学生FaceToken和AccessToken",tags = {"工具"},notes = "有404，200，403，401四种状态")
    public Result<FaceTokenVO> getFaceToken(@PathVariable String id){
        String string=toolsService.getFaceToken(id);

        if (StringUtils.isEmpty(string)){
            return Result.error(null);
        }else {
            faceTokenVO.setFaceToken(string);
            faceTokenVO.setAccessToken(ToolsAipFace.getAccessToken());
            return Result.success(faceTokenVO);
        }
    }


    @PreAuthorize("hasRole('STUDENT')")
    @PatchMapping("/tools/{id}/face")
    @ApiOperation(value = "注册学生faceToken",tags = {"工具"},notes = "有404，200，403，401四种状态")
    public Result<Integer> updateFaceToken(@PathVariable String id, @RequestBody Student student){
        System.out.println(student.getFaceToken());
        if (StringUtils.isEmpty(toolsService.getFaceToken(id))){
            return Result.success(toolsService.updateFaceToken(id,student.getFaceToken()));
        }else{
            return Result.error("你已经注册了FaceID不能重复注册",0);
        }
    }
}
